<template>
  <div class="register-container">
    <!-- 注册内容 -->
    <div class="register">
      <h3>
        注册新用户
        <span class="go">我有账号，去 <a>登陆</a> </span>
      </h3>
      <div class="element_formBox">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          class="demo-ruleForm"
        >
          <el-form-item label="手机号" prop="phone" label-width="100px">
            <el-input v-model="ruleForm.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="验证码" prop="code" label-width="100px">
            <el-input
              v-model="ruleForm.code"
              autocomplete="off"
              style="width: 60%"
            ></el-input>
            <el-button
              type="primary"
              style="width: 39%; margin-left: 1%"
              @click="getCode"
              >发送验证码</el-button
            >
          </el-form-item>
          <el-form-item label="密码" prop="pass" label-width="100px">
            <el-input
              type="password"
              v-model="ruleForm.pass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPass" label-width="100px">
            <el-input
              type="password"
              v-model="ruleForm.checkPass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="" label-width="150px">
            <el-checkbox
              label="我已阅读并同意"
              name="agree"
              v-model="ruleForm.agree"
            ></el-checkbox>
          </el-form-item>
          <el-form-item label-width="160px">
            <el-button type="primary" @click="submitForm('ruleForm')"
              >立即注册</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>

    <!-- 底部 -->
    <div class="copyright">
      <ul>
        <li>关于我们</li>
        <li>联系我们</li>
        <li>联系客服</li>
        <li>商家入驻</li>
        <li>营销中心</li>
        <li>手机尚品汇</li>
        <li>销售联盟</li>
        <li>尚品汇社区</li>
      </ul>
      <div class="address">地址：北京市昌平区宏福科技园综合楼6层</div>
      <div class="beian">京ICP备19006430号</div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Register",
  data() {
    //手机号
    var validatePhone = (rule, value, callback) => {
      if (/^1[34578]{1}\d{9}$/.test(value) == false) {
        callback(new Error("请输入正确的手机号"));
      } else {
        callback();
      }
    };
    // 密码
    var validatePass = (rule, value, callback) => {
      if (/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{6,20}$/.test(value) == false) {
        callback(new Error("请输入6-20位密码，必须包含大小写字母和数字"));
      } else {
        callback();
      }
    };
    // 确认密码
    var validatePass2 = (rule, value, callback) => {
      if (/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{6,20}$/.test(value) == false) {
        callback(new Error("请输入6-20位密码，必须包含大小写字母和数字"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        // 手机号
        phone: "",
        // 验证码
        code: "",
        // 密码
        pass: "",
        // 确认密码
        checkPass: "",
        // 同意
        agree: "",
      },
      rules: {
        // 手机号验证
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { validator: validatePhone, trigger: "blur" },
        ],
        // 验证码验证
        code: [
          { required: true, message: "请输入手机验证码", trigger: "blur" },
          {
            min: 6,
            max: 6,
            message: "请输入正确的手机验证码",
            trigger: "blur",
          },
        ],
        // 密码验证
        pass: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { validator: validatePass, trigger: "blur" },
        ],
        // 确认密码验证
        checkPass: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { validator: validatePass2, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 获取验证码
    async getCode() {
      try {
        // 把phone解构出来
        const { phone } = this.ruleForm;
        // 简单判断一下，phone至少要有值
        let result = await this.$store.dispatch("getCode", phone);
        console.log("验证码", result);
        // 将组件中的code变为仓库中的code
        this.ruleForm.code = this.$store.state.user.code;
      } catch (error) {
        alert(error);
      }
    },
    // 注册用户
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          try {
            const { phone, code, pass } = this.ruleForm;
            let result = await this.$store.dispatch("registeredUser", {
              phone,
              code,
              password: pass,
            });
            console.log(result);
            // 路由跳转到登录
            this.$router.push("/login");
          } catch (error) {
            alert(error);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.register-container {
  .register {
    width: 1200px;
    border: 1px solid rgb(223, 223, 223);
    margin: 0 auto;

    h3 {
      background: #ececec;
      margin: 0;
      padding: 6px 15px;
      color: #333;
      border-bottom: 1px solid #dfdfdf;
      font-size: 20.04px;
      line-height: 30.06px;

      span {
        font-size: 14px;
        float: right;

        a {
          color: #e1251b;
        }
      }
    }

    .element_formBox {
      margin: 40px auto;
      width: 400px;
    }
  }

  .copyright {
    width: 1200px;
    margin: 0 auto;
    text-align: center;
    line-height: 24px;

    ul {
      li {
        display: inline-block;
        border-right: 1px solid #e4e4e4;
        padding: 0 20px;
        margin: 15px 0;
      }
    }
  }
}
</style>